﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using FSY.Components.Entities;
using FSY.Globals;
using FSY.API.DataBase;
using FSY.API.Utilities;
namespace FSY.Components.DataProvider
{
    public class Fsy_SqlDataProvider : Fsy_DataProvider
    {
        private string GetConnectionString()
        {
            return Fsy_Config.GetConnectionString();
        }

        #region Language
        public override int Languages_AddDeleteUpdate(Fsy_Languages languages, DataProviderAction action)
        {
            string sp = "fsy_spLanguages_AddDeleteUpdate";
            return Fsy_SqlHelper.ExecuteNonQuery(this.GetConnectionString(), sp, languages.LanguagesID, languages.CultureCode, languages.CultureName, languages.IsDefault, (int)action);
        }

        public override List<Fsy_Languages> Language_GetListAll()
        {
            string sp = "fsy_spLanguages_GetAll";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, null))
            {
                return Fsy_CBOUtil.FillCollection<Fsy_Languages>(dr);
            }
        }

        public override DataSet Language_GetDataSetAll()
        {
            throw new NotImplementedException();
        }

        public override Fsy_Languages Language_GetByID(int ID)
        {
            string sp = "fsy_spLanguages_GetByLanguagesID";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, ID))
            {
                return (Fsy_Languages)Fsy_CBOUtil.FillObject(typeof(Fsy_Languages), dr);
            }
        }
        #endregion

        #region menu
        public override int Menu_AddDeleteUpdate(Fsy_Menu menu, DataProviderAction action)
        {
            string sp = "fsy_spMenu_AddDeleteUpdate";
            return Fsy_SqlHelper.ExecuteNonQuery(this.GetConnectionString(), sp, menu.MenuID, menu.PortalID, menu.ParentID, menu.MenuName, menu.Url, menu.ThemeID, menu.IsSecure, (int)action);
        }

        public override List<Fsy_Menu> Menu_GetListAll()
        {
            string sp = "fsy_spMenu_GetAll";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, null))
            {
                return Fsy_CBOUtil.FillCollection<Fsy_Menu>(dr);
            }
        }

        public override DataSet Menu_GetDataSetAll()
        {
            string sp = "fsy_spMenu_GetAll";
            return Fsy_SqlHelper.FillDataSet(this.GetConnectionString(), sp, null);
        }

        public override Fsy_Menu Menu_GetByID(int ID)
        {
            string sp = "fsy_spMenu_GetByMenuID";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, ID))
            {
                return (Fsy_Menu)Fsy_CBOUtil.FillObject(typeof(Fsy_Menu), dr);
            }
        }
        #endregion

        #region MenuModule
        public override int MenuModule_AddDeleteUpdate(Fsy_MenuModule menuModule, DataProviderAction action)
        {
            string sp = "fsy_spMenuModule_AddDeleteUpdate";
            return Fsy_SqlHelper.ExecuteNonQuery(this.GetConnectionString(), sp, menuModule.ID, menuModule.MenuID, menuModule.ModuleInstanceID, menuModule.ViewOrder, (int)action);
        }

        public override List<Fsy_MenuModule> MenuModule_GetListAll()
        {
            string sp = "fsy_spMenu_GetAll";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, null))
            {
                return Fsy_CBOUtil.FillCollection<Fsy_MenuModule>(dr);
            }
        }

        public override DataSet MenuModule_GetDataSetAll()
        {
            throw new NotImplementedException();
        }

        public override Fsy_MenuModule MenuModule_GetByID(int ID)
        {
            string sp = "fsy_spMenuModule_GetByID";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, ID))
            {
                return (Fsy_MenuModule)Fsy_CBOUtil.FillObject(typeof(Fsy_MenuModule), dr);
            }
        }
        public override List<Fsy_MenuModule> MenuModule_GetByMenuID(int menuID)
        {
            string sp = "fsy_spMenuModule_GetByMenuID";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, menuID))
            {
                return Fsy_CBOUtil.FillCollection<Fsy_MenuModule>(dr);
            }
        }
        #endregion

        #region MenuPermission
        public override int MenuPermission_AddDeleteUpdate(Fsy_MenuPermission menuPermission, DataProviderAction action)
        {
            string sp = "fsy_spMenuPermission_AddDeleteUpdate";
            return Fsy_SqlHelper.ExecuteNonQuery(this.GetConnectionString(), sp, menuPermission.MenuPermissionID, menuPermission.MenuID, menuPermission.UserID, menuPermission.RoleID, menuPermission.CreatedOnDate, menuPermission.CreatedByUserID, menuPermission.LastModifiedOnDate, menuPermission.LastModifiedByUserID, (int)action);
        }

        public override List<Fsy_MenuPermission> MenuPermission_GetListAll()
        {
            string sp = "fsy_spMenuPermission_GetAll";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, null))
            {
                return Fsy_CBOUtil.FillCollection<Fsy_MenuPermission>(dr);
            }
        }
        public override List<Fsy_MenuPermission> MenuPermission_GetByMenuID(int menuID)
        {
            string sp = "fsy_spMenu_GetByMenuID";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, menuID))
            {
                return Fsy_CBOUtil.FillCollection<Fsy_MenuPermission>(dr);
            }
        }
        public override DataSet MenuPermission_GetDataSetAll()
        {
            throw new NotImplementedException();
        }

        public override Fsy_MenuPermission MenuPermission_GetByID(int ID)
        {
            string sp = "fsy_spMenu_GetByMenuID";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, ID))
            {
                return (Fsy_MenuPermission)Fsy_CBOUtil.FillObject(typeof(Fsy_MenuPermission), dr);
            }
        }
        #endregion

        #region MenuTitle
        public override int MenuTitle_AddDeleteUpdate(Fsy_MenuTitle menuTitle, DataProviderAction action)
        {
            string sp = "fsy_spMenuTitle_AddDeleteUpdate";
            return Fsy_SqlHelper.ExecuteNonQuery(this.GetConnectionString(), sp, menuTitle.MenuID, menuTitle.LanguagesID, menuTitle.Title, menuTitle.Discription, (int)action);
        }

        public override List<Fsy_MenuTitle> MenuTitle_GetListAll()
        {
            string sp = "fsy_spMenuTitle_GetAll";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, null))
            {
                return Fsy_CBOUtil.FillCollection<Fsy_MenuTitle>(dr);
            }
        }

        public override DataSet MenuTitle_GetDataSetAll()
        {
            throw new NotImplementedException();
        }

        public override Fsy_MenuTitle MenuTitle_GetByID(int ID)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region Module
        public override int ModuleControl_AddDeleteUpdate(Fsy_ModuleControl moduleControle, DataProviderAction action)
        {
            string sp = "fsy_spModuleControl_AddDeleteUpdate";
            return Fsy_SqlHelper.ExecuteNonQuery(this.GetConnectionString(), sp, moduleControle.ModuleID, moduleControle.ControlTitle, moduleControle.ControlSrc, moduleControle.Description, moduleControle.IsActive, moduleControle.CreatedOnDate, moduleControle.CreatedByUserID, moduleControle.LastModifiedOnDate, moduleControle.LastModifiedByUserID, (int)action);
        }

        public override List<Fsy_ModuleControl> ModuleControl_GetListAll()
        {
            string sp = "fsy_spModuleControl_GetAll";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, null))
            {
                return Fsy_CBOUtil.FillCollection<Fsy_ModuleControl>(dr);
            }
        }

        public override System.Data.DataSet ModuleControl_GetDataSetAll()
        {
            throw new NotImplementedException();
        }

        public override Fsy_ModuleControl ModuleControl_GetByID(int ID)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region Portal
        public override int Portal_AddDeleteUpdate(Fsy_Portal portal, DataProviderAction action)
        {
            string sp = "fsy_spPortal_AddDeleteUpdate";
            return Fsy_SqlHelper.ExecuteNonQuery(this.GetConnectionString(), sp, portal.PortalID, portal.PortalName, portal.Discription, portal.CreatedOnDate, portal.CreatedByUserID, portal.LastModifiedOnDate, portal.LastModifiedByUserID, (int)action);
        }

        public override List<Fsy_Portal> Portal_GetListAll()
        {
            string sp = "fsy_spPortal_GetAll";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, null))
            {
                return Fsy_CBOUtil.FillCollection<Fsy_Portal>(dr);
            }
        }

        public override System.Data.DataSet Portal_GetDataSetAll()
        {
            throw new NotImplementedException();
        }

        public override Fsy_Portal Portal_GetByID(int ID)
        {
            string sp = "fsy_spPortal_GetByPortalID";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, ID))
            {
                return (Fsy_Portal)Fsy_CBOUtil.FillObject(typeof(Fsy_Portal), dr);
            }
        }
        #endregion

        #region Role
        public override int Role_AddDeleteUpdate(Fsy_Role role, DataProviderAction action)
        {
            string sp = "fsy_spRole_AddDeleteUpdate";
            return Fsy_SqlHelper.ExecuteNonQuery(this.GetConnectionString(), sp, role.RoleID, role.RoleName, role.RoleParent, role.PortalID, role.CreatedOnDate, role.CreatedByUserID, role.LastModifiedOnDate, role.LastModifiedByUserID, (int)action);
        }

        public override List<Fsy_Role> Role_GetListAll()
        {
            string sp = "fsy_spRole_GetAll";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, null))
            {
                return Fsy_CBOUtil.FillCollection<Fsy_Role>(dr);
            }
        }

        public override DataSet Role_GetDataSetAll()
        {
            string sp = "fsy_spRole_GetAll";
            return Fsy_SqlHelper.FillDataSet(this.GetConnectionString(), sp, null);
        }

        public override Fsy_Role Role_GetByID(int ID)
        {
            string sp = "fsy_spRole_GetByRoleID";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, ID))
            {
                return (Fsy_Role)Fsy_CBOUtil.FillObject(typeof(Fsy_Role), dr);
            }
        }
        public override List<Fsy_RoleQuery> Role_GetByUser(int user)
        {
            string sp = "fsy_spRole_GetByRoleUser";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, user))
            {
                return Fsy_CBOUtil.FillCollection<Fsy_RoleQuery>(dr);
            }
        }

        public override List<Fsy_RoleQuery> Role_GetByMenu(int menu)
        {
            string sp = "fsy_spRole_GetByRoleMenu";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, menu))
            {
                return Fsy_CBOUtil.FillCollection<Fsy_RoleQuery>(dr);
            }
        }

        public override List<Fsy_RoleQuery> Role_GetByModule(int module)
        {
            string sp = "fsy_spRole_GetByRoleModule";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, module))
            {
                return Fsy_CBOUtil.FillCollection<Fsy_RoleQuery>(dr);
            }
        }
        #endregion

        #region RolePermission
        public override int RolePermission_AddDeleteUpdate(Fsy_RolePermission rolePermiss, DataProviderAction action)
        {
            string sp = "";
            return Fsy_SqlHelper.ExecuteNonQuery(this.GetConnectionString(), sp, rolePermiss.RoleID, rolePermiss.PermissionID, (int)action);
        }

        public override List<Fsy_RolePermission> RolePermission_GetListAll()
        {
            string sp = "";
            IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, null);
            return Fsy_CBOUtil.FillCollection<Fsy_RolePermission>(dr);
        }

        public override DataSet RolePermission_GetDataSetAll()
        {
            string sp = "";
            return Fsy_SqlHelper.FillDataSet(this.GetConnectionString(), sp, null);
        }
        #endregion

        #region Theme
        public override int Theme_AddDeleteUpdate(Fsy_Theme theme, DataProviderAction action)
        {
            string sp = "fsy_spTheme_AddDeleteUpdate";
            return Fsy_SqlHelper.ExecuteNonQuery(this.GetConnectionString(), sp, theme.ThemeID, theme.ThemeName, theme.IsSystem, theme.IsDefault, theme.Description, theme.Image, (int)action);
        }

        public override List<Fsy_Theme> Theme_GetListAll()
        {
            string sp = "fsy_spTheme_GetAll";
            IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, null);
            return Fsy_CBOUtil.FillCollection<Fsy_Theme>(dr);
        }

        public override DataSet Theme_GetDataSetAll()
        {
            string sp = "fsy_spTheme_GetAll";
            return Fsy_SqlHelper.FillDataSet(this.GetConnectionString(), sp, null);
        }

        public override Fsy_Theme Theme_GetByID(int ID)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region User
        public override int User_AddDeleteUpdate(Fsy_User user, DataProviderAction action)
        {
            string sp = "fsy_spUser_AddDeleteUpdate";
            return Fsy_SqlHelper.ExecuteNonQuery(this.GetConnectionString(), sp, user.UserID, user.UserName, Fsy_SecurityUtil.Encrypt(user.Password, "fsy"), user.FirstName, user.LastName, user.IsSuperUser, user.Email, user.DisplayName, user.LastIPAddress, user.CreatedOnDate, user.CreatedByUserID, user.LastModifiedOnDate, user.LastModifiedByUserID, user.MobilePIN, user.PasswordQuestion, user.PasswordAnswer, user.LastLoginDate, user.IsLockOut, user.LastLockOutDate, user.LastPasswordChanged, user.Avatar, user.Picture, (int)action);
        }

        public override List<Fsy_User> User_GetListAll()
        {
            string sp = "fsy_spUser_GetAll";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, null))
            {
                if (dr != null)
                    return Fsy_CBOUtil.FillCollection<Fsy_User>(dr);
                return null;
            }
        }
        public override IDataReader User_GetAll()
        {
            string sp = "fsy_spUser_GetAll";
            return Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, null);
        }
        public override DataSet User_GetDataSetAll()
        {
            string sp = "fsy_spUser_GetAll";
            return Fsy_SqlHelper.FillDataSet(this.GetConnectionString(), sp, null);
        }

        public override Fsy_User User_GetByID(int ID)
        {
            string sp = "fsy_spUser_GetByUserID";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, ID))
            {
                if (dr != null)
                    return (Fsy_User)Fsy_CBOUtil.FillObject(typeof(Fsy_User), dr);
                return null;
            }
        }

        public override List<Fsy_UserQuery> User_GetByRole(int roleID)
        {
            string sp = "fsy_spUser_GetByRole";
            using (IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, roleID))
            {
                if (dr != null)
                    return Fsy_CBOUtil.FillCollection<Fsy_UserQuery>(dr);
                return null;
            }
        }

        public override List<Fsy_UserQuery> User_GetByMenu(int menuID)
        {
            string sp = "fsy_spUser_GetByMenu";
            IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, menuID);
            if (dr != null)
                return Fsy_CBOUtil.FillCollection<Fsy_UserQuery>(dr);
            return null;
        }

        public override List<Fsy_UserQuery> User_GetByModule(int moduleID)
        {
            string sp = "fsy_spUser_GetByModule";
            IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, moduleID);
            if (dr != null)
                return Fsy_CBOUtil.FillCollection<Fsy_UserQuery>(dr);
            return null;
        }
        #endregion

        #region userRole
        public override int UserRole_AddDeleteUpdate(Fsy_UserRole userRole, DataProviderAction action)
        {
            string sp = "";
            return Fsy_SqlHelper.ExecuteNonQuery(this.GetConnectionString(), sp, userRole.UserRoleID, userRole.UserID, userRole.RoleID, userRole.ExpiryDate, userRole.IsTrialUsed, userRole.EffectiveDate, userRole.CreatedOnDate, userRole.CreatedByUserID, userRole.LastModifiedOnDate, userRole.LastModifiedByUserID, (int)action);
        }

        public override List<Fsy_UserRole> UserRole_GetListAll()
        {
            string sp = "";
            IDataReader dr = Fsy_SqlHelper.ExecuteReader(this.GetConnectionString(), sp, null);
            return Fsy_CBOUtil.FillCollection<Fsy_UserRole>(dr);
        }

        public override DataSet UserRole_GetDataSetAll()
        {
            string sp = "";
            return Fsy_SqlHelper.FillDataSet(this.GetConnectionString(), sp, null);
        }

        public override Fsy_UserRole UserRole_GetByID(int ID)
        {
            throw new NotImplementedException();
        }
        #endregion

    }
}
